package com.itheima.controller;

import com.itheima.anno.MyLog;
import com.itheima.dto.EmpQueryRequest;
import com.itheima.dto.PageBean;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import com.itheima.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping( "/emps")
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result<PageBean> page(EmpQueryRequest queryRequest) {
        PageBean<Emp> empPageBean = empService.listQueryEmpPage(queryRequest);

        return Result.success(empPageBean);
    }

    @PostMapping
    public Result<String> save(@RequestBody Emp emp) {
        empService.save(emp);
        log.info("数据{}", emp.getExprList());
        return Result.success("新增成功");
    }

    @DeleteMapping
    @MyLog
    public Result<String> delete(@RequestParam List<Integer> ids) {
        empService.delete(ids);
        return Result.success("删除成功");
    }

    @GetMapping("/{id}")
    public Result<Emp> getInfoById(@PathVariable Integer id) {
        Emp e=empService.getInfoById(id);
        return Result.success(e);
    }

    @PutMapping
    public Result<String> update(@RequestBody Emp emp) {
        if (emp.getId()==null) {
            return Result.error("缺少参数");
        }
        empService.update(emp);
        return Result.success("修改成功");
    }

    /**
     * 查询班主任信息
     * @return
     */
    @GetMapping("/list")
    public Result<Emp> getClazz() {
        List<Emp> clazzList=empService.getClazz();
        return Result.success(clazzList);
    }

}
